<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx URL 重写与重定向完全指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e293b;
            color: #e2e8f0;
            border-radius: 12px;
            position: relative;
            overflow: hidden;
        }
        
        .code-block::before {
            content: 'nginx';
            position: absolute;
            top: 10px;
            right: 15px;
            font-size: 12px;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -8px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 20px;
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        .highlight-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        
        .floating {
            animation: float 3s ease-in-out infinite;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6 relative">
        <div class="max-w-6xl mx-auto relative z-10">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                    Nginx URL 重写与重定向
                </h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                    掌握现代 Web 服务器的核心技能，优雅处理网站迁移、SEO 优化与用户体验
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-exchange-alt text-3xl mb-2 floating"></i>
                        <p class="text-sm opacity-80">灵活重写</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-directions text-3xl mb-2 floating" style="animation-delay: 0.5s"></i>
                        <p class="text-sm opacity-80">智能重定向</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-chart-line text-3xl mb-2 floating" style="animation-delay: 1s"></i>
                        <p class="text-sm opacity-80">SEO 优化</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-16">
        <!-- Introduction -->
        <div class="content-card rounded-2xl p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                在 Nginx 中配置 URL 重写和重定向是常见的需求，尤其是在处理网站迁移、SEO优化、以及用户友好URL等方面。本指南将深入探讨这两个核心概念，帮助您掌握 Nginx 的强大功能。
            </p>
        </div>

        <!-- URL Rewrite Section -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <div class="mb-8">
                <div class="feature-icon">
                    <i class="fas fa-code"></i>
                </div>
                <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">URL 重写</h2>
            </div>
            
            <p class="text-lg text-gray-700 mb-8 leading-relaxed">
                URL 重写是指将请求的 URL 修改为另一个 URL 以便更好地处理请求。Nginx 提供了 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">rewrite</code> 指令用于 URL 重写。
            </p>

            <div class="bg-gray-50 rounded-xl p-6 mb-8">
                <h3 class="text-xl font-semibold text-gray-800 mb-4">
                    <i class="fas fa-terminal text-purple-600 mr-2"></i>基本语法
                </h3>
                <div class="code-block p-6">
                    <pre><code>rewrite regex replacement [flag];</code></pre>
                </div>
                
                <div class="mt-6 space-y-3">
                    <div class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <p><code class="bg-purple-100 text-purple-700 px-2 py-1 rounded text-sm">regex</code>：匹配请求 URL 的正则表达式</p>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <p><code class="bg-purple-100 text-purple-700 px-2 py-1 rounded text-sm">replacement</code>：将匹配到的 URL 替换为的新 URL</p>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt-1 mr-3"></i>
                        <p><code class="bg-purple-100 text-purple-700 px-2 py-1 rounded text-sm">flag</code>：控制重写行为的标志（可选）</p>
                    </div>
                </div>
            </div>

            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-xl p-6">
                    <h4 class="text-lg font-semibold text-gray-800 mb-4">
                        <i class="fas fa-file-alt text-purple-600 mr-2"></i>示例 1：页面重写
                    </h4>
                    <p class="text-gray-700 mb-4">将 <code class="bg-white px-2 py-1 rounded text-sm">/old-page</code> 重写为 <code class="bg-white px-2 py-1 rounded text-sm">/new-page</code></p>
                    <div class="code-block p-4 text-sm">
                        <pre><code>location /old-page {
    rewrite ^/old-page$ /new-page permanent;
}</code></pre>
                    </div>
                </div>

                <div class="bg-gradient-to-br from-blue-50 to-cyan-50 rounded-xl p-6">
                    <h4 class="text-lg font-semibold text-gray-800 mb-4">
                        <i class="fas fa-folder text-blue-600 mr-2"></i>示例 2：路径重写
                    </h4>
                    <p class="text-gray-700 mb-4">URL 路径替换示例</p>
                    <div class="code-block p-4 text-sm">
                        <pre><code>location / {
    rewrite ^/demo/(.*)$ /demo/$1 last;
}</code></pre>
                    </div>
                </div>
            </div>

            <div class="bg-gradient-to-r from-purple-100 to-pink-100 rounded-xl p-6">
                <h4 class="text-lg font-semibold text-gray-800 mb-4">
                    <i class="fas fa-lightbulb text-yellow-600 mr-2"></i>使用场景
                </h4>
                <div class="grid md:grid-cols-3 gap-4">
                    <div class="bg-white rounded-lg p-4 text-center">
                        <i class="fas fa-search text-3xl text-purple-600 mb-3"></i>
                        <h5 class="font-semibold mb-2">SEO 优化</h5>
                        <p class="text-sm text-gray-600">保留搜索引擎排名</p>
                    </div>
                    <div class="bg-white rounded-lg p-4 text-center">
                        <i class="fas fa-user-friends text-3xl text-pink-600 mb-3"></i>
                        <h5 class="font-semibold mb-2">用户友好 URL</h5>
                        <p class="text-sm text-gray-600">简化复杂的 URL 结构</p>
                    </div>
                    <div class="bg-white rounded-lg p-4 text-center">
                        <i class="fas fa-cogs text-3xl text-blue-600 mb-3"></i>
                        <h5 class="font-semibold mb-2">动态请求处理</h5>
                        <p class="text-sm text-gray-600">映射到不同处理逻辑</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- URL Redirect Section -->
        <div class="content-card rounded-2xl p-10 mb-12">
            <div class="mb-8">
                <div class="feature-icon">
                    <i class="fas fa-directions"></i>
                </div>
                <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">URL 重定向</h2>
            </div>
            
            <p class="text-lg text-gray-700 mb-8 leading-relaxed">
                URL 重定向是指将请求的 URL 转发到另一个 URL。Nginx 使用 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">return</code> 指令或 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">rewrite</code> 指令的重定向标志来实现。
            </p>

            <div class="bg-gray-50 rounded-xl p-6 mb-8">
                <h3 class="text-xl font-semibold text-gray-800 mb-4">
                    <i class="fas fa-terminal text-purple-600 mr-2"></i>基本语法
                </h3>
                <div class="code-block p-6">
                    <pre><code>return code url;</code></pre>
                </div>
                
                <div class="mt-6 space-y-3">
                    <div class="flex items-start">
                        <i class="fas fa-chevron-right text-purple-600 mt